import {
  getNiuzhuanList,
  getTypeListByCompanyId
} from "@/formRules/common/autoComplete";
import { formateDate } from "@/utils/date";
import {
  companySelect,
  companyListSelect,
  companyFirstSelect
} from "../common";
import store from "@/store";
import { required } from "../validators";
export const fangshiFrom = () => {
  return [
    { value: "1", label: "派单" },
    { value: "2", label: "补单" },
    { value: "4", label: "自建工单" }
  ];
};
export const toMoke = (v, length = 20) => {
  let data = "";
  if (!v) {
    data = "无";
  } else if (v && v.length > length) {
    data = v.slice(0, length) + "...";
  } else {
    data = v;
  }
  return `<div class="pointer retive moke-box">
	${data}
	<div class="moke-text absolute pd10 bg-chart color-white size16 ${
    v ? "" : "hidden"
  }">${v}</div>
	</div>
	`;
};
// 满意度
export const orderMydList = () => {
  return [
    { label: "满意", value: "0" },
    { label: "不满意", value: "1" }
    // { label: "未评价", value: null }
  ];
};
// 后台事件等级
export const dengjiListAll = () => {
  return [
    { value: 2, label: "一级事件" },
    {
      value: 3,
      label: "二级事件"
    },
    { value: 1, label: "三级事件" }
  ];
};
export const workFrom = () => {
  return [
    { value: "电话报障", label: "电话报障" },
    {
      value: "二维码报障",
      label: "二维码报障"
    },
    { value: "小程序报障", label: "小程序报障" },
    { value: "现场报障", label: "现场报障" }
    // { value: "补单", label: "补单" }
  ];
};
export const typeListAll = () => {
  return [
    { label: "全部", value: "" },
    { label: "处理中", value: "0" },
    { label: "转单中", value: "1" },
    { label: "审核中", value: "2" },
    { label: "已完成", value: "3" },
    { label: "已挂起", value: "4" },
    { label: "待响应", value: "5" },
    { label: "待到场", value: "6" },
    { label: "转单完成", value: "7" },
    { label: "待评价", value: "8" },
    { label: "未完成", value: "9" },
    { label: "已取消", value: "10" }
  ];
};

export const searchRule = (levIdList, optionsList) => {
  return [
    companyFirstSelect(
      {
        title: "单位",
        options: [
          //   {
          //     value: "",
          //     label: "全部",
          //     parentCompanyId: "",
          //     companyName: "全部"
          //   }
        ].concat(store.state.userCompanyList),
        props: {
          filterable: true,
          clearable: false,
          "collapse-tags": true,
          multiple: true
        },
        field: "companyTreeIdList",
        value: []
      },
      true
    ),
    companyListSelect(
      {
        options: optionsList ? optionsList : [].concat(store.state.companyList)
      },
      true
    ),
    {
      type: "select",
      field: "state",
      title: "状态",
      options: [].concat(typeListAll()),
      value: "",
      col: {
        span: 4
      }
    },
    {
      type: "select",
      field: "levId",
      title: "事件等级",
      options: [],
      value: "",
      col: {
        span: 4
      }
    },

    {
      type: "input",
      field: "nowDealUser",
      title: "处理人",
      value: "",
      col: {
        span: 4
      }
    },
    // {
    //   type: "input",
    //   field: "customName",
    //   title: "处理人姓名",
    //   value: "",
    //   col: {
    //     span: 4
    //   }
    // },
    {
      type: "datePicker",
      field: "planTime",
      title: "时间",
      col: {
        span: 4
      },
      props: {
        type: "datetimerange",
        format: "yyyy-MM-dd HH:mm:ss",
        startPlaceholder: "开始时间",
        endPlaceholder: "结束时间"
      },
      filter: v => {
        return Moment(v).format("YYYY-MM-DD HH:mm:ss");
      },
      tableVisible: true,
      value: [
        formateDate(
          new Date(new Date().toLocaleDateString()) - 24 * 30 * 60 * 60 * 1000
        ),
        formateDate(new Date())
      ]
    },

    {
      type: "select",
      field: "isVip",
      title: "用户属性",
      options: [],
      col: {
        span: 4
      },
      value: "",
      tableVisible: true
    },
    {
      type: "select",
      field: "source",
      title: "工单来源",
      value: "",
      col: {
        span: 4
      },
      options: [{ label: "全部", value: "" }].concat(workFrom()),
      tableVisible: true
    },
    {
      type: "select",
      field: "fangshi",
      title: "工单方式",
      value: "",
      col: {
        span: 4
      },
      options: [{ label: "全部", value: "" }].concat(fangshiFrom()),
      tableVisible: true
    },
    {
      type: "select",
      field: "orderMyd",
      title: "满意度",
      options: [{ label: "全部", value: "" }].concat(orderMydList()),
      value: "",
      col: {
        span: 4
      }
    },
    {
      type: "input",
      field: "createUser",
      title: "发起人",
      col: {
        span: 4
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "paidanren",
      title: "派单人",
      col: {
        span: 4
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "fenleiName",
      title: "资产分类",
      col: {
        span: 4
      },
      value: "",
      tableVisible: true
    },
    {
      type: "select",
      field: "chaoshigongdan",
      title: "是否超时",
      col: {
        span: 4
      },
      options: [
        { value: "", label: "全部" },
        { value: "超时工单", label: "超时工单" },
        { value: "不超时工单", label: "不超时工单" }
      ],
      value: "",
      tableVisible: true
    }
  ];
};

export const editRule = isAdmin => {
  return [
    {
      type: "hidden",
      hidden: true,
      field: "deviceId",
      title: "deviceId",
      value: "",
      tableVisible: false
    },

    {
      type: "hidden",
      hidden: true,
      field: "goujianId",
      title: "goujianId",
      value: "",
      tableVisible: false
    },
    {
      type: "hidden",
      hidden: isAdmin ? false : true,
      field: "companyTreeName",
      title: "单位名称",
      value: "",
      tableVisible: true,
      width: 110
    },
    {
      type: "hidden",
      hidden: isAdmin ? false : true,
      field: "companyName",
      title: "单位办公点",
      value: "",
      tableVisible: true,
      width: 110
    },
    {
      type: "input",
      field: "title",
      title: "工单名称",
      width: 210,
      value: "",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      tableVisible: true,
      html: v => {
        return `<div class="${
          v.indexOf("已预警") > -1
            ? "color-warning"
            : v.indexOf("已超时") > -1
            ? "color-danger"
            : ""
        }">
 <span>${v}</span>
      	</div>`;
      }
    },
    {
      type: "input",
      field: "createTime",
      width: 155,
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "创建时间",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "source",
      title: "工单来源",
      width: 100,
      value: "",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      tableVisible: true
    },

    {
      type: "select",
      field: "status",
      title: "工单方式",
      value: "",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      options: fangshiFrom(),
      filter: v => {
        return fangshiFrom().find(e => e.value == v)?.label;
      },
      tableVisible: true
    },
    {
      field: "propertyName",
      type: "input",
      title: "资产分类",
      tableVisible: true,
      hidden: true
    },
    // {
    //   field: "deviceName",
    //   type: "input",
    //   title: "设备名称",
    //   tableVisible: true,
    //   hidden: true
    // },
    {
      type: "input",
      field: "createUser",
      title: "报障人号码",
      col: {
        span: 8
      },
      width: 110,
      props: {
        disabled: true
      },
      value: "",
      tableVisible: true,
      hidden: true
    },
    {
      field: "baozhangName",
      title: "报障人姓名",
      width: 100,
      validate: [],
      props: {
        placeholder: ""
      },
      tableVisible: true,
      hidden: true,
      type: "input"
    },
    {
      type: "input",
      field: "isVip",
      title: "用户属性",
      props: {
        disabled: true
      },
      col: {
        span: 8
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "miaoshu",
      title: "故障描述",
      width: 110,
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      tableVisible: true,
      hidden: true,
      html: h => {
        return toMoke(h);
      }
    },
    // {
    //   type: "input",
    //   field: "deviceName",
    //   title: "报修资产",
    //   value: "",
    //   col: {
    //     span: 8,
    //   },
    //   props: {
    //     disabled: true,
    //   },
    //   tableVisible: true,
    // },
    {
      type: "select",
      field: "state",
      title: "工单状态",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      options: typeListAll().splice(1),
      html: v => {
        return `<div class="${
          v == 3
            ? "color-success"
            : v == 0
            ? "color-warning"
            : v == "已报废"
            ? "color-danger"
            : ""
        }">${
          typeListAll()
            .filter(e => e.label != "全部")
            .find(i => i.value === v)?.label
        }
			
		</div>`;
      },
      tableVisible: true
    },
    {
      type: "select",
      field: "level",
      title: "事件等级",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      options: dengjiListAll(),
      filter: v => {
        return dengjiListAll().find(i => i.value === v)?.label || v;
      },
      tableVisible: false
    },
    {
      type: "input",
      field: "levName",
      title: "事件等级",
      props: {
        disabled: true
      },
      col: {
        span: 8
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "nowDealUser",
      title: "工程师账号",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      width: 110,
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "customName",
      title: "工程师姓名",
      width: 120,
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "chuliResult",
      title: "处理结果",
      width: 130,
      col: {
        span: 8
      },
      props: {
        disabled: true,
        type: "textarea"
      },
      value: "",
      tableVisible: true,
      html: h => {
        return toMoke(h);
      }
    },
    {
      type: "input",
      field: "paidanren",
      title: "派单人",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "paidanrenName",
      title: "派单姓名",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "paidanhaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "服务台响应耗时",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "fwtCchaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "服务台响应超时",
      value: "",
      tableVisible: true
    },
    // {
    //   type: "input",
    //   field: "xyhaoshi",
    //   col: {
    //     span: 8
    //   },
    //   props: {
    //     disabled: true
    //   },
    //   title: "工程师响应耗时",
    //   value: "",
    //   tableVisible: true
    // },
    {
      type: "input",
      field: "dchaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "到场耗时",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "dcchaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "到场超时",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "chulihaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "处理耗时",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "chulichaoshi",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      title: "处理超时",
      value: "",
      tableVisible: true
    },
    {
      type: "select",
      field: "orderMyd",
      col: {
        span: 8
      },
      props: {
        disabled: true
      },
      options: orderMydList(),
      html: v => {
        return [{ label: "未评价", value: null }]
          .concat(orderMydList())
          .find(e => e.value == v)?.label;
      },
      title: "评价状态",
      value: "",
      tableVisible: true
    },
    {
      type: "input",
      field: "pjms",
      col: {
        span: 8
      },
      props: {
        disabled: true,
        type: "textarea"
      },
      title: "评价描述",
      value: "",
      width: 110,
      tableVisible: true
    }
    // {
    //   type: "input",
    //   field: "preConditioningTime",
    //   col: {
    //     span: 8
    //   },
    //   props: {
    //     disabled: true
    //   },
    //   title: "预处理时间",
    //   value: "",
    //   tableVisible: true
    // }
  ];
};

export const niuRule = (isAdmin, orderId) => {
  return [
    companySelect(
      {
        options: [].concat(store.state.companyList),
        col: {
          span: 24
        },
        props: {
          filterable: true
        }
      },
      isAdmin
    ),
    {
      type: "select",
      field: "groupList",
      title: "运维分组",
      value: "",
      options: [],
      tableVisible: false,
      hidden: false
    },
    {
      type: "select",
      field: "dealUser",
      title: "工程师账号",
      value: "",
      validate: [required()],
      props: {
        filterable: true
      },
      options: [],
      tableVisible: false,
      hidden: false
    },

    {
      type: "input",
      field: "workorderId",
      title: "workorderId",
      value: orderId || "",
      tableVisible: false,
      hidden: true
    }
  ];
};
export const checkPlayForm = () => {
  return [
    {
      field: "dealUser",
      placeholder: "请选择处理人",
      title: "处理人",
      type: "select",
      options: [],
      type_Persons: true,
      rules: [
        {
          message: "请选择处理人",
          trigger: "onBlur"
        }
      ]
    }
  ];
};

export const chuliForm = () => {
  return [
    {
      field: "hiddenImg",
      placeholder: "请选择图片",
      title: "照片",
      type: "FormListImageFile",
      options: [],
      rules: [
        {
          message: "请选择图片",
          trigger: "onBlur"
        }
      ]
    },
    {
      field: "hiddenDesc",
      placeholder: "请填写描述",
      title: "描述",
      type: "input",
      rows: 3,
      options: [],
      props: {
        type: "textarea"
      }
    }
  ];
};

export const instaShotForm = () => {
  return [
    {
      field: "propertyName",
      title: "资产分类",
      type: "input",
      props: {
        disabled: false
      },
      col: {
        span: 12
      }
    },
    {
      field: "deviceName",
      title: "资产名称",
      type: "input",
      props: {
        type: "text",
        disabled: false
      },
      col: {
        span: 12
      }
    },
    {
      field: "address",
      title: "报障地址",
      type: "input",
      props: {
        type: "textarea",
        disabled: false
      },
      col: {
        span: 12
      }
    },
    {
      field: "instaShotsDec",
      placeholder: "请填写故障描述",
      title: "故障描述",
      rows: 3,
      type: "input",
      props: {
        type: "textarea",
        disabled: false
      },
      rules: [
        {
          message: "请填写故障描述",
          trigger: "onBlur"
        }
      ]
    },
    {
      field: "instaShotImg",
      placeholder: "",
      title: "报障照片",
      maxCount: 10,
      props: {
        disabled: false
      },
      value: [],
      type: "FormListImageFile"
    }
  ];
};
export const shenheForm = () => {
  return [
    // {
    //   field: "instaShotsDec",
    //   placeholder: "请填写审核结果描述",
    //   title: "审核结果描述",
    //   rows: 3,
    //   type: "textarea",
    //   rules: [
    //     {
    //       message: "请填写描述",
    //       trigger: "onBlur"
    //     }
    //   ]
    // }
  ];
};
//后台需要字段
export const regionFrom = () => {
  return [
    {
      field: "companyName",
      title: "单位办公点",
      type: "input",
      props: {
        options: [],
        placeholder: "请选择单位办公点",
        title: "单位办公点"
      },
      col: {
        span: 12
      }
    },

    {
      field: "gangWei",
      title: "岗位",
      type: "input",
      props: {
        options: [],
        placeholder: "请填写岗位",
        title: "岗位"
      },
      col: {
        span: 12
      }
    },
    {
      field: "bumenName",
      title: "部门名称",
      type: "input",
      props: {
        options: [],
        placeholder: "请填写部门名称",
        title: "部门名称"
      },
      col: {
        span: 12
      }
    },
    {
      field: "createUser",
      placeholder: "请输入报障账号",
      title: "报障账号",
      type: "input",
      props: {
        disabled: false,
        row: 1
      },
      col: {
        span: 12
      }
    },
    {
      field: "createCustomName",
      placeholder: "请输入报障姓名",
      title: "报障姓名",
      type: "input",
      props: {
        disabled: false,
        row: 1
      },
      col: {
        span: 12
      }
    }
  ];
};

export const wikiEditRule = () => {
  return [
    {
      type: "hidden",
      hidden: true,
      field: "eventId",
      title: "eventId",
      value: "",
      tableVisible: false
    },
    {
      type: "hidden",
      hidden: true,
      field: "fieldList",
      title: "fieldList",
      value: [],
      tableVisible: false
    },
    {
      type: "input",
      field: "content",
      title: "知识节点",
      value: "",
      tableVisible: true
    },
    // {
    //   type: "input",
    //   field: "categoryName",
    //   title: "知识类型",
    //   value: "",
    //   tableVisible: true,
    //   hidden: true
    // },
    // {
    //   type: "input",
    //   field: "categoryId",
    //   title: "知识类型",
    //   value: 7,
    //   tableVisible: true,
    //   hidden: true
    // },
    {
      type: "input",
      field: "keyWord",
      title: "关键字",
      value: "工单类型",
      tableVisible: true
    },
    {
      type: "input",
      field: "introduction",
      title: "简介",
      value: "工单知识库",
      tableVisible: true
    },
    {
      type: "input",
      field: "isPrivate",
      title: "是否公开",
      value: "1",
      hidden: true,
      tableVisible: true
    },
    {
      type: "datePicker",
      field: "createTime",
      title: "发布时间",
      value: "",
      tableVisible: true,
      hidden: true
    }
  ];
};

export const tongyongRule = () => {
  return [
    {
      field: "instaShotsDec",
      title: "故障内容说明",
      type: "input",
      validate: [required()],
      props: {
        type: "textarea",
        placeholder: "请填写故障内容说明"
      }
    },
    {
      field: "instaShotImg",
      placeholder: "",
      title: "故障图片",
      maxCount: 10,
      rows: 2,
      type: "FormListImageFile",
      rules: [
        {
          message: "请选择图片",
          trigger: "onBlur"
        }
      ],
      value: []
    },
    {
      field: "user",
      title: "报修人信息",
      validate: [required()],
      props: {
        placeholder: "请输入报修人信息"
      },
      type: "input"
    },
    {
      field: "baozhangName",
      title: "报障人姓名",
      validate: [],
      props: {
        placeholder: "请输入报障人姓名"
      },
      type: "input"
    },
    {
      field: "baozhangAddress",
      title: "报障地址",
      validate: [],
      props: {
        placeholder: "请输入报障地址",
        type: "textarea"
      },
      type: "input"
    },
    {
      field: "yunweiUser",
      title: "运维人员",
      props: {
        placeholder: "运维人员填写时相当运维人员补单"
      },
      type: "select",
      options: [],
      props: {
        filterable: true
      }
      //   control: {
      //     handle: function(val, fApi) {
      //       return val;
      //     },
      //     rule: [

      //     ]
      //   }
    },
    {
      field: "levId",
      title: "事件等级",
      props: {
        placeholder: "请选择事件等级"
      },
      options: [],
      hidden: false,
      validate: [required()],
      type: "select",
      value: ""
    },
    {
      field: "paidanren",
      title: "派单人账号",
      props: {
        placeholder: "请输入派单人账号"
      },
      hidden: true,
      type: "input"
    }
  ];
};
export const createShotForm = () => {
  return [
    {
      field: "source",
      title: "工单来源",
      props: {
        placeholder: "请选择工单来源"
      },
      options: workFrom().shift(),
      value: "小程序报障",
      hidden: false,
      type: "select"
    },
    companySelect(
      {
        options: [].concat(store.state.companyList),
        props: {
          filterable: true,
          clearable: false
        },
        col: { span: 24 },
        validate: [required()]
      },
      true
    ),
    getTypeListByCompanyId(
      "propertyType",
      "资产分类",
      {
        type: "cascader",
        validate: {
          required: true,
          message: "请选择资产分类!"
        },
        props: { disabled: false, options: [] },
        value: [1, 7],
        col: {
          span: 24
        },
        hidden: false,
        isHaveOptions: true
      },
      {
        companyId: store.state.companyId
          ? Number(store.state.companyId)
          : store.state.companyList.length
          ? store.state.companyList[0].value
          : null
      }
    ),
    ...tongyongRule()
  ];
};

/**
 * @name 关闭订单
 */
export const clooseRules = () => {
  return [
    {
      field: "eventId",
      title: "eventId",
      hidden: true,
      type: "hidden"
    },
    {
      field: "createUser",
      title: "操作人",
      hidden: true,
      type: "input"
    },
    {
      field: "closeReason",
      title: "取消理由",
      props: {
        placeholder: "请输入取消理由",
        type: "textarea"
      },
      value: "用户自己已解决",
      hidden: false,
      type: "input",
      validate: [required()]
    }
  ];
};
